<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home/list' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 表格 -->
    <!-- 
      el-table : 表格
        data : 表格的数据
      el-table-column : 列   
        prop : 读取数据 
        label : 每一列的标题
     -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="序号" width="50"> </el-table-column>
      <el-table-column prop="title" label="标题" width="180"> </el-table-column>
      <el-table-column prop="user.nickname" label="作者"> </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_date | date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="封面">
        <template slot-scope="scope">
          <img :src="fixedUrl(scope.row.cover[0].url)" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="del(scope.row.id)"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!-- 
      el-pagination 分页组件 
        background : 页码才有背景颜色
        layout="prev, pager, next"
        total : 总共数目
     -->
    <el-pagination
      :page-size="pageSize"
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="currentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [], // 文章列表  10
      pageIndex: 1,
      pageSize: 3,
      total: 0, // 总共数据条数
    }
  },
  created() {
    this.getPostlist()
  },
  methods: {
    // 获取文章列表
    async getPostlist() {
      let res = await this.$axios.get('/post', {
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        },
      })

      console.log('文章列表', res.data)
      this.tableData = res.data.data
      this.total = res.data.total
    },
    // 删除文章
    del(id) {
      console.log('文章', id)
    },
    // 点击页码
    currentChange(cur) {
      console.log(cur) // 拿到当前页码
      this.pageIndex = cur
      this.getPostlist()
    },
    fixedUrl(url) {
      if (url.startsWith('http')) {
        return url
      } else {
        return this.$axios.defaults.baseURL + url
      }
    },
  },
}
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin: 20px 0;
}

img {
  width: 100px;
  height: 90px;
  object-fit: cover;
}
</style>
